/**
 * @format
 * @Author: TC
 * @Date: 2025-05-29 10:47:24
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-07 12:16:11
 */

import XLSX from "xlsx";
export class ExcelUtil {
    /**
     * 读取某一行
     * @param worksheet
     * @param rowNum
     * @returns
     */
    static getRowInSheet(worksheet: XLSX.WorkSheet, rowNum: number): unknown[] {
        const range = XLSX.utils.decode_range(worksheet["!ref"] || "A1:A1");
        if (rowNum - 1 > range.e.r) {
            throw new Error(`行号超出范围: ${rowNum}`);
        }
        // 读取指定行的数据
        const rowData: unknown[] = [];
        const rowIndex = rowNum; // 转换为0-based索引

        for (let c = range.s.c; c <= range.e.c; c++) {
            const cellAddress = XLSX.utils.encode_cell({ r: rowIndex, c });
            const cell = worksheet[cellAddress];

            // 获取单元格的值（优先使用格式化后的值，否则使用原始值）
            const cellValue = cell ? cell.w || cell.v : undefined;
            rowData.push(cellValue);
        }
        return rowData;
    }
}
